<?php
/**
 * Created by PhpStorm.
 * User: wang
 * Date: 2016/7/29
 * Time: 10:20
 */
namespace Home\Controller;
use Org\Util\Date;
use Think\Controller\RestController;

class UserController extends RestController
{
    public function login_get(){
        echo 'login';
    }

    public function login_post(){
        $user=M('user')->where(array(
            'phone_no'=>I('post.phone_no'),
            'password'=>md5(I('post.password'))
        ))->find();
        if($user){
            session('user_id',$user['id']);
            $this->ajaxReturn($user);
        }else{
            $this->ajaxReturn(array(
                'status'=>'error',
                'info'=>'用户名或密码错误'
            ));
        }
    }
    public function index_get($id=0){
        $user=M('user')->where('id='.$id)->find();
        $user['parks']=M('parking')->where('user_id='.$id)->select();
        $this->ajaxReturn($user);
    }
    public function index_post($id=0){
        $data=array(
            'phone_no'=>I('post.phone_no'),
            'password'=>md5(I('post.password')),
            'status'=>1,
            'name'=>I('post.name'),
            'nickname'=>I('post.nickname'),
            'sex'=>I('post.sex',0),
            'control_lock'=>I('post.control_lock'),
            'last_login_time'=>new Date().'',
            'balance'=>0
        );
        if($id){
            $res=M('user')->where('id='.$id)->save($data);
        }else{
            $res=M('user')->add($data);
        }

        if($res){
            $this->ajaxReturn($data);
        }else{
            $this->ajaxReturn(array('status'=>'error','info'=>'错误'));
        }
    }
}